﻿using System;
using System.Diagnostics;
using System.IO;

namespace SfsTools.Core
{
    /// <summary>
    /// Represents an unusable index entry.
    /// </summary>
    internal class BadSectorEntry : IndexEntry
    {
        #region Properties

        /// <summary>
        /// Gets or sets the starting block number of the bad sector region in the data area.
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">The argument <paramref name="value"/> is out of valid range.</exception>
        public Int64 FirstBlock
        {
            [DebuggerStepThrough]
            get
            {
                using (BinaryReader br = new BinaryReader(new MemoryStream(FBuffer, false)))
                {
                    br.BaseStream.Position = 0x0A;
                    return br.ReadInt64();
                }
            }
            [DebuggerStepThrough]
            set
            {
                if (value < 0)
                    throw new ArgumentOutOfRangeException("value");
                using (BinaryWriter bw = new BinaryWriter(new MemoryStream(FBuffer, true)))
                {
                    bw.BaseStream.Position = 0x0A;
                    bw.Write(value);
                }
            }
        }

        /// <summary>
        /// Gets or sets the ending block number of the bad sector region in the data area.
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">The argument <paramref name="value"/> is out of valid range.</exception>
        public Int64 LastBlock
        {
            [DebuggerStepThrough]
            get
            {
                using (BinaryReader br = new BinaryReader(new MemoryStream(FBuffer, false)))
                {
                    br.BaseStream.Position = 0x12;
                    return br.ReadInt64();
                }
            }
            [DebuggerStepThrough]
            set
            {
                if (value < 0)
                    throw new ArgumentOutOfRangeException("value");
                using (BinaryWriter bw = new BinaryWriter(new MemoryStream(FBuffer, true)))
                {
                    bw.BaseStream.Position = 0x12;
                    bw.Write(value);
                }
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Accepts a visitor.
        /// </summary>
        /// <param name="visitor">The visitor.</param>
        [DebuggerStepThrough]
        public override void AcceptVisitor(IIndexEntryVisitor visitor)
        {
            visitor.VisitBadSector(this);
        }

        #endregion

        #region Constructors
        /// <overloads>Initializes a new instance of <see cref="BadSectorEntry"/>.</overloads>
        /// <summary>
        /// Initializes a new instance of <see cref="BadSectorEntry"/>.
        /// </summary>
        [DebuggerStepThrough]
        public BadSectorEntry(long position, byte[] buffer)
            : base(position, buffer)
        {
        }
        #endregion
    }
}
